package com.restart.game.controller;

import com.restart.game.common.bean.result.ResultBean;
import com.restart.game.common.util.filter.FieldFilter;
import com.restart.game.common.util.response.ResultUtil;
import com.restart.game.entity.Info;
import com.restart.game.service.InfoService;
import com.restart.game.service.InfoTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ReStartLin
 * @data 2018/12/7 11:44
 * @classDesc: 功能描述:
 */
@RequestMapping("/api/info")
@Controller
public class InfoController {
    public static final Logger logger = LoggerFactory.getLogger(InfoController.class);

    private final InfoService infoService;

    private final InfoTypeService infoTypeService;

    @Autowired
    public InfoController(InfoService infoService, InfoTypeService infoTypeService) {
        this.infoService = infoService;
        this.infoTypeService = infoTypeService;
    }

    @PostMapping("/search")
    public String index(HttpServletRequest request) {
        logger.info("search");
        String selectHtml = request.getParameter("selectHtml");//搜索的网站 id
        String ipGameName = request.getParameter("ipGameName");//游戏名字
        String ipPageNum = request.getParameter("ipPageNum"); //页数
        logger.info("请求参数为: html:{}  name:{}  page:{}", selectHtml, ipGameName, ipPageNum);
        //目前和以后或许只支持97973 所以不做扩展
        if (selectHtml.equals("1")) {//true always
            if (!FieldFilter.checkName(ipGameName)) {
                logger.error("{}游戏不允许搜索", ipGameName);
                return "info/success";
            }
            infoService.searchGame97973(ipGameName, Integer.parseInt(ipPageNum));
        }
        return "info/success";
    }


    @ResponseBody
    @RequestMapping("game/{name}")
    public ResultBean getInfo(@PathVariable("name") String name, HttpServletResponse response) throws IOException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        //获得当前游戏在数据库里面的信息
        //根据游戏名字获得游戏id
        Integer id = infoTypeService.getId(name);
        if (id == null) {
            return ResultUtil.error("该游戏不存在");
        }
        Info infoByTypeID = infoService.getInfoByTypeID(id);
        if (infoByTypeID == null) {
            return ResultUtil.error("没有可用的信息了");
        }
        //获得子页信息
        if (infoByTypeID.getContent()!=null) {
            return ResultUtil.successBody(infoByTypeID);
        }
        Info info = infoService.getHtml(infoByTypeID);
        infoService.updateByPrimaryKey(info);
        return ResultUtil.successBody(info);
    }

    @ResponseBody
    @GetMapping("success/{id}")
    public ResultBean updateInfo(@PathVariable("id") Integer id, HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Origin", "*");
        if (id != 0) {
            Info info = infoService.deleteInfoByID(id);
            if (info != null) {
                infoTypeService.changeData(info.getTypeId(), true);
            }
        }
        return ResultUtil.success("更新成功");
    }

    @ResponseBody
    @GetMapping("fail/{id}")
    public ResultBean deleteInfo(@PathVariable("id") Integer id, HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Origin", "*");
        if (id != 0) {
            Info info = infoService.deleteInfoByID(id);
            if (info != null) {
                infoTypeService.changeData(info.getTypeId(), false);
            }
        }
        return ResultUtil.success("更新成功");
    }

    @ResponseBody
    @GetMapping("next/{name}")
    public ResultBean nextInfo(@PathVariable("name") String name,HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Origin", "*");
        if (!name.equals("")) {
            Integer id = infoTypeService.getId(name);
            if (id == null) {
                return ResultUtil.success();
            }
            Info infoByTypeID = infoService.getInfoByTypeID(id);
            if (infoByTypeID == null) {
                return ResultUtil.success();
            }
            infoByTypeID.setFlag(true);
            infoService.updateByPrimaryKey(infoByTypeID);
            infoTypeService.changeData(infoByTypeID.getTypeId(), false);
        }
        return ResultUtil.success();
    }

}
